#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys
from fabric.api import *
from fabric.contrib.files import exists

BASE_PATH = '/home/xu.du/apps/'
APP_PATH = BASE_PATH + 'stock'
APP_NAME = 'download_pdf.py'

POOL_SIZE = 10

@parallel(pool_size=POOL_SIZE)
def deploy_app():
    if not exists(APP_PATH):
        run('mkdir -p ' + APP_PATH)
    with settings(warn_only=True):
        sudo('rm -Rf ' + APP_PATH + '/*')
    put('*', APP_PATH + '/')


@parallel(pool_size=POOL_SIZE)
def run_app():
    with cd(APP_PATH):
        run('$(nohup python {app} >& /dev/null < /dev/null &) && sleep 1'.format(app=APP_NAME), shell='/bin/bash')


@parallel(pool_size=POOL_SIZE)
def stop():
    with settings(warn_only=True):
        run('pkill -f ' + APP_NAME)


@parallel(pool_size=POOL_SIZE)
def test():
    # run('pip list | grep ' + sys.argv[1])
    with settings(warn_only=True):
        # sudo('pip install pdfminer')
        sudo('pip install git+https://git.oschina.net/socialcredits/social-credits-py --upgrade')


if __name__ == "__main__":
    all_vps = [
        'xu.du@121.199.23.3',
        'xu.du@121.199.4.6',
        'xu.du@121.199.2.152',
        'xu.du@121.199.12.190',
        'xu.du@121.41.53.230',
        'xu.du@121.199.5.96',
        'xu.du@121.199.20.87',
        'xu.du@121.199.12.245',
        'xu.du@121.40.93.44',
        'xu.du@121.199.22.228',
        'xu.du@120.26.94.198',
        'xu.du@120.26.94.202',
        'xu.du@120.26.94.146',
        'xu.du@120.26.94.163',
        'xu.du@120.26.94.211',
        'xu.du@120.26.94.117',
        'xu.du@120.26.94.195',
        'xu.du@120.26.94.207',
        'xu.du@120.26.94.185',
        'xu.du@120.26.93.249',
        'xu.du@120.26.94.17',
        'xu.du@120.26.94.5',
        'xu.du@120.26.94.7',
        'xu.du@120.26.93.202',
        'xu.du@120.26.94.188',
        'xu.du@120.26.94.35',
        'xu.du@120.26.94.58',
        'xu.du@120.26.94.120',
        'xu.du@120.26.94.203',
        'xu.du@120.26.94.38',
        'xu.du@120.26.94.150',
        'xu.du@120.26.94.151',
        'xu.du@120.26.94.147',
        'xu.du@120.26.94.28',
        'xu.du@120.26.94.191',
        'xu.du@120.26.94.18',
        'xu.du@120.26.93.254',
        'xu.du@120.26.94.49',
        'xu.du@120.26.94.139',
        'xu.du@120.26.94.2',
        'xu.du@120.26.94.4',
        'xu.du@120.26.94.23',
        'xu.du@120.26.94.29',
        'xu.du@120.26.94.174',
        'xu.du@120.26.94.8',
        'xu.du@120.26.93.240',
        'xu.du@120.26.93.215',
        'xu.du@120.26.94.122',
        'xu.du@120.26.94.12',
        'xu.du@120.26.92.125',
        'xu.du@120.26.92.180',
        'xu.du@120.26.93.219',
        # 'xu.du@120.26.94.76',
        'xu.du@120.26.93.229',
        'xu.du@120.26.94.22',
        'xu.du@120.26.94.14',
        'xu.du@120.26.94.84',
        'xu.du@120.26.94.27',
        'xu.du@120.26.93.221',
        'xu.du@121.43.60.236',
        'xu.du@121.41.1.166',
        'xu.du@120.26.217.236',
        'xu.du@120.26.92.73',
        'xu.du@120.55.180.251',
        'xu.du@120.26.91.2',
        'xu.du@120.26.223.152',
        'xu.du@120.26.223.135',
        'xu.du@120.26.91.8',
        'xu.du@120.55.112.92',
        'xu.du@120.55.181.10',
    ]
    fault_vps = [
        'xu.du@120.26.93.221',
        'xu.du@121.41.1.166',
        'xu.du@120.55.112.92',
    ]
    env.hosts = all_vps
    env.password = 'Sunday.2016'
    # execute(deploy_app)
    execute(run_app)
    # execute(stop)
    # execute(test)


# http://epaper.jwb.com.cn/jwb/html/2016-02/01/node_1.htm